const {
	db,
	dbCmd,
	result_success,
	result_error,
	appletInfoCollection,
	collectionCollection,
	userCollection
} = require('../../common/constants')
module.exports = async function(params = {}) {
	const token = this.getUniIdToken()
	if (!token) {
		return result_error('登录失效,请重新登录')
	}
	let payload = await this.uniID.checkToken(token)
	if (payload.code) {
		return payload
	}
	let type = params.type
	let cUser = await userCollection.doc(payload.uid).get()
	cUser = cUser.data[0]
	if(!cUser.app_id){
		return result_success({})
	}
	//获取当前应用
	let appletInfoData = await appletInfoCollection.doc(cUser.app_id).get()
	appletInfoData = appletInfoData.data[0]
	let app_id = appletInfoData._id
	let wechat_id = appletInfoData.wechat.gh_id
	let alipay_id = appletInfoData.alipay.app_id
	let share_title = "收款"
	let share_summary = ''
	let share_path = 'pages/collection/collection'
	let share_image = 'https://mp-d84e928e-2b27-4fa9-b240-63574c62e78a.cdn.bspapp.com/QQ截图20240408210058.png'
	let share_web = "https://mall.web.wmeizi.com/skipapplet.html?appId=" + alipay_id
	let quickCollection = await db.collection('wmzmall-quick-collection').where({
		shop_id: app_id
	}).get()
	quickCollection = quickCollection.data[0]
	if (!quickCollection) {
		quickCollection = {
			app_id: app_id,
			shop_id: app_id,
			vip: false,
			status: false,
			recharge: false,
			collection: [],
			create_time: new Date().getTime(),
			creator_id: payload.uid
		}
		await db.collection('wmzmall-quick-collection').add(quickCollection)

	}
	if (!type) {
		let collectionDatas = await collectionCollection.where({
				app_id: app_id,
				shop_id: app_id,
				status: '进行中'
			})
			.limit(100)
			.orderBy('create_time', 'desc')
			.get()
		collectionDatas = collectionDatas.data
		for (var i in collectionDatas) {
			if (!collectionDatas[i].checked) {
				collectionDatas[i].checked = false
			}
		}
		quickCollection.collectionDatas = collectionDatas
	}
	let collection = quickCollection.collection
	for (var i in collection) {
		share_title = collection[i].name + (collection[i].money / 100).toFixed(2) + "元,待支付"
		if (collection[i].share_title) {
			share_title = collection[i].share_title
		}
		if (collection[i].share_image) {
			share_image = collection[i].share_image
		}
		if (collection[i].share_summary) {
			share_summary = collection[i].share_summary
		}
		collection[i].share_title = share_title
		collection[i].share_image = share_image
		collection[i].share_summary = share_summary
		collection[i].wechat_id = wechat_id
		collection[i].share_path = share_path
		collection[i].share_web = share_web
		collection[i].WXSceneSession = true
		collection[i].WXSceneTimeline = false
	}
	quickCollection.collection = collection
	return result_success(quickCollection)
}